package com.wyh.service.serviceImpl.gfiveImpl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.common.result.Result;
import com.wyh.domain.po.gfive.Invoicerecords;
import com.wyh.domain.vo.gfive.InvoicerecordsVO;
import com.wyh.domain.vo.gfive.InvoicereordsSelectVo;
import com.wyh.domain.vo.gfive.PushCustomersVO;
import com.wyh.domain.vo.gfive.PushInvcPartVO;
import com.wyh.mapper.gfivemapper.InvoicerecordsDTOMapper;
import com.wyh.mapper.gfivemapper.InvoicerecordsVOMapper;
import com.wyh.service.gfive.InvoicerecordsServiceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InvoicerecordsServiceVOImpl extends ServiceImpl<InvoicerecordsVOMapper, InvoicerecordsVO>
        implements InvoicerecordsServiceVO {

    @Resource
    private InvoicerecordsVOMapper InMapper;

    @Resource
    private InvoicerecordsDTOMapper InDTOMapper;


    @Override
    public Result showPage(Integer pageNum, Integer pageSize) {
        List<InvoicerecordsVO> invoicerecordsVOS = InMapper.showPage(pageNum, pageSize);
        int Size = InMapper.InvoicerecorNumber();
        return Result.success(invoicerecordsVOS, Long.valueOf(Size));
    }

    /**
     * 推送 即返回手机号 以及邮箱
     */
    @Override
    public Result lookCustomer(String invoicecode) {
        //判断订单是否存在
        int i = InMapper.slectInvoicecode(invoicecode);
        if (i == 0) {
            return Result.error("发票代码错误，请重新输入！！");
        }
        PushCustomersVO pushCustomersVO = InMapper.selectCustomer(invoicecode);
        if (pushCustomersVO == null) {
            return Result.error("账单已撤销无法推送");
        }
        return Result.success(pushCustomersVO);
    }


    @Override
    public Result selectInvoicerecords(InvoicereordsSelectVo invoicerecordsSelectVO) {
        //先查询票据类型的id
        Integer TypeNum = InDTOMapper.selectInvByname(invoicerecordsSelectVO.getInvoicingtype());
        log.info("票据类型的id{}", TypeNum);
        //查询业主
        Integer Owner = InDTOMapper.selectOwner(invoicerecordsSelectVO.getOwner());
        log.info("业主{}", Owner);

        //查询购买方
        Integer Buyer = InDTOMapper.selectByuer(invoicerecordsSelectVO.getBuyer());
        log.info("购买方{}", Buyer);

        List<Invoicerecords> invoicerecords = InDTOMapper.selectData(invoicerecordsSelectVO, TypeNum, Owner, Buyer);
        if (invoicerecords == null || invoicerecords.isEmpty()) {
            return Result.error("查询数据不存在！！");
        }


        ArrayList<InvoicerecordsVO> list = new ArrayList<>();
        for (int i = 0; i < invoicerecords.size(); i++) {
            PushInvcPartVO pushInvcPartVO = InMapper.selectPart(invoicerecords.get(i).getInvoicingtype(),
                                                                invoicerecords.get(i).getOwner(),
                                                                invoicerecords.get(i).getBuyer(),
                                                                invoicerecords.get(i).getNoticenumber());
            log.info("查询出来的数据{}", pushInvcPartVO);
            InvoicerecordsVO invoicerecordsVO1 = new InvoicerecordsVO(
                    invoicerecords.get(i).getSerialnumber(),
                    invoicerecords.get(i).getNoticenumber(),
                    pushInvcPartVO.getName(),
                    invoicerecords.get(i).getInvoicecode(),
                    invoicerecords.get(i).getInvoicenumber(),
                    pushInvcPartVO.getCustomername(),
                    pushInvcPartVO.getBuyer(),
                    invoicerecords.get(i).getInvoicingtime(),
                    invoicerecords.get(i).getAmount());
            list.add(invoicerecordsVO1);
        }

        //将两个 list  invoicerecordsv 合并为一个Stream流
//        List<Object> collect1 = Stream.concat(invoicerecords.stream(), list.stream()).collect(Collectors.toList());
//        String serialnumber1 = ((Invoicerecordes) collect1).getSerialnumber();
//        log.info("serialnumber1===={}",serialnumber1);
//        String name1 = ((PushInvcPartVO) collect1).getName();
//        log.info("名字为：{}",name1);
//        List<InvoicerecordsVO> collect = collect1.stream().map(
//                Invo -> {
//                    String serialnumber = ((Invoicerecordes) Invo).getSerialnumber();
//                    String noticenumber = ((Invoicerecordes) Invo).getNoticenumber();
//                    String name = ((PushInvcPartVO) Invo).getName();
//                    String invoicecode = ((Invoicerecordes) Invo).getInvoicecode();
//                    String invoicenumber = ((Invoicerecordes) Invo).getInvoicenumber();
//                    String customername = ((PushInvcPartVO) Invo).getCustomername();
//                    String buyer = ((PushInvcPartVO) Invo).getBuyer();
//                    Date invoicingtime = ((Invoicerecordes) Invo).getInvoicingtime();
//                    Double amount = ((Invoicerecordes) Invo).getAmount();
//                    return new InvoicerecordsVO(serialnumber, noticenumber, name, invoicecode, invoicenumber, customername, buyer, invoicingtime, amount);
//
//                }
//        ).collect(Collectors.toList());


        return Result.success(list);
    }
}
